Ext.define('ZYY.controller.system.role',{
    extend : 'Ext.app.Controller',
    views:['system.role','system.roleEdit'],
    stores:['system.role'],
    refs:[
    {
        ref: 'role', 
        selector: '', 
        xtype: 'sysrole',  
        autoCreate: true
    }
    ],
    init:function(application){
        if (this.inited) {
            return;
        }
        this.inited = true;
        this.control({
            "sysrole":{
                selectionchange:this.selchg
            },
            "sysrole toolbar button[action=add]":{
                click:this.add
            },
            "sysrole toolbar button[action=update]":{
                click:this.update
            },
            "sysrole toolbar button[action=delete]":{
                click:this.del
            },
            "roleEdit  button[action=save]":{
                click:this.saveRole
            }
        });
        console.log('控制器'+this.$className+'初始化');
    },
    selchg:function(sm, selections){
        (Ext.ComponentQuery.query('sysrole toolbar button[action=delete]')[0]).setDisabled(selections.length != 1);
        (Ext.ComponentQuery.query('sysrole toolbar button[action=update]')[0]).setDisabled(selections.length != 1);
    },
    actionIndex: function() {
        this.application.setMainView(this.getRole());
    },
    add:function(){
        Ext.widget('roleEdit',{
            title:"新增角色",
            iconCls:"role_add"
        });
    },
    update:function(button){
        var edit = Ext.widget('roleEdit',{
            title:"修改角色",
            iconCls:"role_edit"
        });
        var record = button.up("gridpanel").getSelectionModel().getSelection()[0];
        edit.down('form').loadRecord(record);
    },
    del:function(button){
        var record = button.up("gridpanel").getSelectionModel().getSelection()[0];
        var store = button.up("gridpanel").getStore();
        Ext.MessageBox.confirm('提示', '确认删除该角色吗？', function(btn){
            if(btn=="yes"){
                Ext.Ajax.request({
                    url: path+"/admin/role/delete.htm",
                    params: {
                        id:record.data.roleid
                    },
                    success: function(response){
                        var data = Ext.Number.from(response.responseText);
                        if(data>0){
                            Ext.MessageBox.alert('提示', "删除成功！");
                            store.remove(record);
                        }
                    }
                });
				  	
            }
		    	
        });
    },
    saveRole:function(button){
        var win=button.up("window"),
         form = win.down('form').getForm(),
         flag=form.findField("roleid").getValue(),
         store=this.getRole().getStore(),
         selModel=this.getRole().getSelectionModel(),
         record=selModel.getSelection()[0],
         index=store.indexOf(record),
         url;
        console.log(flag);
        if(flag){
            url =  path+"/admin/role/update.htm";
        }else{
            url =  path+"/admin/role/add.htm";
        }
        if(form.isValid()){
            form.submit({
                url:url,
                success: function(f, o) {
                    if(flag){
                        store.remove(record);
                        store.insert(index,form.getValues());
                        selModel.select(index);
                    }else{
                        form.findField("roleid").setValue(o.result.roleid);
                        store.insert(store.getCount(),form.getValues());
                    }
                    form.reset();
                    win.close();
                    Ext.MessageBox.alert('提示', "操作成功！");
                }
            });
        }
    }
});